package com.mpdbailey.cleverdicandroid;

import java.util.List;

import android.os.AsyncTask;

import com.pigdogbay.androidutils.diagnostics.Timing;

public class AnagramPresenter
{
	IAnagramView _View;
	WordList _WordList;
	boolean _DictionaryLoaded = false, _Searching = false, _FindSubAnagrams=true;
	
	public void SetFindSubAngrams(boolean value)
	{
		_FindSubAnagrams = value;
	}
	public void SetResultLimit(int limit)
	{
		_WordList.SetResultLimit(limit);
	}
	public void SetDictionary(List<String> wordList)
	{
		_WordList.SetWordList(wordList);
		_DictionaryLoaded = wordList!=null;
	}

	public AnagramPresenter(IAnagramView view)
	{
		_View = view;
		_WordList = new WordList();
	}

	public void Clear()
	{
		_View.clearMatches();
		_View.setStatus("",false);
		_View.setQuery("");
		_View.setError(null);
	}


	private boolean IsQueryOK(String query)
	{
		if (isNullOrBlank(query))
		{
			_View.setError(_View.getStringResource(R.string.EntryErrorMessage));
			return false;
		}
		if (!_DictionaryLoaded)
		{
			_View.setError(_View.getStringResource(R.string.DictionaryNotLoaded));
			return false;
		}
		_View.setError(null);
		return true;
	}

	public void Search()
	{
		if (_Searching)
		{
			return;
		}
		String query = _View.getQuery();
		query = _WordList.ConvertQuery(query);
		if (!IsQueryOK(query))
		{
			return;
		}
		_View.clearMatches();
		_Searching = true;
		_View.setStatus(_View.getStringResource(R.string.Searching),true);
		this.new BackgroundWorker().execute(query);
	}

	private static boolean isNullOrBlank(String param)
	{
		return param == null || param.trim().length() == 0;
	}
	/*
	 * Removes formatting such as missing letters
	 * eg 'be (e)' becomes 'be'
	 */
	public static String RemoveFormatting(String result)
	{
		int index = result.indexOf(" (");
		return index==-1 ? result : result.substring(0,index);
	}
	

	private class BackgroundWorker extends AsyncTask<String, String, Void> implements WordListCallback
	{
		int numOfMatches = 0;
		
		@Override
		protected Void doInBackground(String... params)
		{
			Timing timing = new Timing();
			try
			{
				String query = params[0];
				numOfMatches = 0;
				_WordList.RunQuery(query,BackgroundWorker.this);
				if (	_FindSubAnagrams 
						&& !query.contains(" ")
						&& !query.contains("#")
						&& !_WordList.IsPartialWord(query) 
						&& query.length() > 2
						&& query.length() < 20)
				{
					_WordList.FindSubAnagrams(query, BackgroundWorker.this);
				}
			} catch (Exception e)
			{
			}
			timing.LogDuration("CleverDic");
			return null;
		}
		@Override
		protected void onProgressUpdate(String... values)
		{
			_View.addMatch(values[0]);
		}

		@Override
		protected void onPostExecute(Void result)
		{
			_View.setStatus(_View.getStringResource(R.string.MatchesFound)+Integer.valueOf(numOfMatches).toString(),false);
			_Searching = false;
		}
		@Override
		public void Update(String result)
		{
			numOfMatches++;
			publishProgress(result);
			
		}
	}
}
